package com.atguigu.gmall.all.controller;


import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class CartController {

    @Autowired
    CartFeignClient cartFeignClient;

    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){
        CartInfo cartInfo = cartFeignClient.addCart(skuId, skuNum);
        //model.addAttribute("cartInfo",cartInfo);
        redirectAttributes.addAttribute("skuId",skuId);
        redirectAttributes.addAttribute("skuNum",skuNum);
        return "redirect:http://www.gmall.com/toCart.html";
    }

    @GetMapping("/toCart.html")
    public String toCart (Long skuId ,Integer skuNum,Model model){
        CartInfo cartInfo = cartFeignClient.toCart(skuId);
        if (cartInfo != null){
            cartInfo.setSkuNum(skuNum);
            model.addAttribute("cartInfo",cartInfo);
            return "cart/addCart";
        }else {
            return "redirect:http://www.gmall.com";
        }

    }

    //去购物车结算
    @GetMapping("/cart.html")
    public String cart(){
        return "cart/index";
    }
}
